package com.lin.filegraph.utils.parser.astutil;

import com.lin.filegraph.utils.methoddg.MethodGraph;
import com.lin.filegraph.utils.filedg.FileGraph;
import com.lin.filegraph.utils.parser.node.JProjectNode;
import com.lin.filegraph.utils.parser.simpleast.SimpleASTCreator;

/**
 * @description:
 * @author: linhuaixu
 * @time: 2023/5/18 11:06
 */

public class JSourceParser {
    ASTFileASTRequestor astFileASTRequestor = null;

    public ASTFileASTRequestor createAST(String projectPath) {
        astFileASTRequestor = new ASTFileASTRequestor();
        ASTCreator astCreator = new ASTCreator(projectPath, astFileASTRequestor);
        astCreator.createASTs();
        return astFileASTRequestor;
    }

    public JProjectNode createSimpleAST(String projectName, String versionNo, String projectPath) {
        if (astFileASTRequestor == null || !astFileASTRequestor.getProjectPath().equals(projectPath)) {
            astFileASTRequestor = createAST(projectPath);
        }

        SimpleASTCreator simpleASTCreator = new SimpleASTCreator(projectName, versionNo, projectPath,
                astFileASTRequestor);
        System.out.println("JDT ast created...");
        JProjectNode root = simpleASTCreator.createSimpleAST();
        root.setMdg(new MethodGraph(root));
        root.setFdg(new FileGraph(root));
        root.setProjectPath(projectPath);
//		root.setCompDG(new ComponentGraph(projectPath));

        return root;
    }
    public JProjectNode createNullSimpleAST(String projectName, String versionNo, String projectPath) {
        if (astFileASTRequestor == null || !astFileASTRequestor.getProjectPath().equals(projectPath)) {
            astFileASTRequestor = createAST(projectPath);
        }
        SimpleASTCreator simpleASTCreator = new SimpleASTCreator(projectName, versionNo, projectPath,
                astFileASTRequestor);
        System.out.println("JDT ast created...");
        JProjectNode root = simpleASTCreator.createNullSimpleAST();
        return root;
    }
}

